use std::borrow::{Borrow, Cow};

fn remove_blank(s: &str) -> Cow<str> {
    if s.contains(' ') {
        let mut buf = String::with_capacity(s.len());
        for c in s.chars() {
            if c != ' ' {
                buf.push(c);
            }
        }

        return Cow::Owned(buf);
    }

    return Cow::Borrowed(s);
}

fn abs_all(vs: &mut Cow<[i32]>) {
    for i in 0..vs.len() {
        if vs[i] < 0 {
            vs.to_mut()[i] = -vs[i];
        }
    }
}

#[test]
fn test_remove() {
    assert_eq!("abcde", remove_blank(" abc d e"));

    let input = vec![1, 2, 3];
    abs_all(&mut Cow::from(&input));
    assert_eq!(vec![1, 2, 3], input);

    let mut input = Cow::from(vec![1, -2, -3]);
    abs_all(&mut input);
    let br: &[i32] = input.borrow();
    assert_eq!(vec![1, 2, 3], br);
}
